package com.github.wui.circle;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.github.wui.R;
import com.github.wui.utils.ViewUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义圆角线性布局
 *
 * @author Mr.xie
 * @Date 2025/4/10
 */
public class CircleConstraintLayout extends ConstraintLayout {

    private float round;
    private int bkColor;

    private boolean isOpenGradient = false;
    private int startColor;
    private int centerColor;
    private int endColor;


    /**
     * 是否开启多颜色渐变
     */
    private boolean mut_grandiet = false;

    private String mut_grandiet_colors;
    private static final int Linear = 0;

    private static final int T_B = 0;
    private static final int L_R = 1;
    private static final int B_T = 2;
    private static final int R_L = 3;
    private static final int BR_TL = 4;
    private static final int BL_TR = 5;
    private static final int TL_BR = 6;
    private static final int TR_BL = 7;

    private int grandientType = 0;

    private List<Integer> colors = new ArrayList<>();

    private int linearGranditentDir = L_R;

    private int borderColor = Color.TRANSPARENT;
    private float borderWidth = 0;
    /**
     * 是否关闭全圆角设置模式
     */
    private boolean isCloseFullRoundMode = false;

    private float leftTopRound = 0f;
    private float leftBottomRound = 0f;
    private float rightTopRound = 0f;
    private float rightBottomRound = 0f;
    public CircleConstraintLayout(Context context) {
        this(context, null);
    }

    public CircleConstraintLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleConstraintLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleLinearLayout);
        round = typedArray.getDimension(R.styleable.CircleLinearLayout_round, 0);
        bkColor = typedArray.getColor(R.styleable.CircleLinearLayout_bkColor, Color.BLACK);
        startColor = typedArray.getColor(R.styleable.CircleLinearLayout_startColor, -1);
        centerColor = typedArray.getColor(R.styleable.CircleLinearLayout_centerColor, -1);
        endColor = typedArray.getColor(R.styleable.CircleLinearLayout_endColor, -1);
        isOpenGradient = typedArray.getBoolean(R.styleable.CircleLinearLayout_isOpenGradient, false);
        this.grandientType = typedArray.getInt(R.styleable.CircleLinearLayout_gradientType, Linear);
        this.linearGranditentDir = typedArray.getInt(R.styleable.CircleLinearLayout_linear_dir, L_R);
        this.mut_grandiet = typedArray.getBoolean(R.styleable.CircleLinearLayout_mut_grandiet, false);
        this.mut_grandiet_colors = typedArray.getString(R.styleable.CircleLinearLayout_mur_grandient_color);
        this.borderColor = typedArray.getColor(R.styleable.CircleConstraintLayout_wui_border_color, Color.TRANSPARENT);
        this.borderWidth = typedArray.getDimension(R.styleable.CircleConstraintLayout_wui_border_width, 0f);


        this.rightBottomRound = typedArray.getDimension(R.styleable.CircleTextView_rightBottomRound,0f);
        this.leftBottomRound = typedArray.getDimension(R.styleable.CircleTextView_leftBottomRound,0f);
        this.rightTopRound = typedArray.getDimension(R.styleable.CircleTextView_rightTopRound,0f);
        this.leftTopRound = typedArray.getDimension(R.styleable.CircleTextView_leftTopRound ,0f);
        this.isCloseFullRoundMode = typedArray.getBoolean(R.styleable.CircleRelativeLayout_isCloseFullRoundMode, false);
        typedArray.recycle();
        setWillNotDraw(false);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        try {
            Drawable background = getBackground();
            if (background != null) {
                this.setBackground(background);
                super.onDraw(canvas);
                return;
            }
            if (isOpenGradient) {
                if (this.mut_grandiet) {  // 多颜色渐变
                    String[] colors = this.mut_grandiet_colors.split(";");
                    int[] finalColors = new int[colors.length];
                    for (int i = 0; i < colors.length; i++) {
                        finalColors[i] = Color.parseColor(colors[i]);
                    }
                    doGrandient(finalColors);
                } else {
                    doGrandient(defualtColors());
                }
            }else {
                RectF rectF = new RectF();
                rectF.left = 0;
                rectF.top = 0;
                rectF.right = getMeasuredWidth();
                rectF.bottom = getMeasuredHeight();
                @SuppressLint("DrawAllocation") Paint bkPaint = new Paint();
//                bkPaint.setColor(bkColor);
                GradientDrawable gradientDrawable = new GradientDrawable();
                gradientDrawable.setStroke((int) borderWidth, borderColor);
                if (isCloseFullRoundMode){
                    ViewUtils.setRound(gradientDrawable ,leftTopRound , rightTopRound , rightBottomRound ,leftBottomRound);
                }else {
                    gradientDrawable.setCornerRadius(round);
                }
                gradientDrawable.setColor(bkColor);
                setBackground(gradientDrawable);
//                canvas.drawRoundRect(rectF, this.round, this.round, bkPaint);
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        super.onDraw(canvas);

    }

    @NonNull
    private int[] defualtColors() {
        if (startColor != -1) colors.add(startColor);
        if (centerColor != -1) colors.add(centerColor);
        if (endColor != -1) colors.add(endColor);
        int[] finalColors = new int[colors.size()];
        for (int i = 0; i < colors.size(); i++) {
            finalColors[i] = colors.get(i);
        }
        return finalColors;
    }


    /**
     * 布局线性渐变
     */
    private void doGrandient(int[] colors) {
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setOrientation(GradientDrawable.Orientation.LEFT_RIGHT);
        if (isCloseFullRoundMode){
            ViewUtils.setRound(gradientDrawable ,leftTopRound , rightTopRound , rightBottomRound ,leftBottomRound);
        }else {
            gradientDrawable.setCornerRadius(round);
        }
        gradientDrawable.setGradientType(this.grandientType);

        gradientDrawable.setColors(colors);
        gradientDrawable.setStroke((int) borderWidth, borderColor);
        switch (this.linearGranditentDir) {
            case L_R:
                gradientDrawable.setOrientation(GradientDrawable.Orientation.LEFT_RIGHT);
                break;
            case R_L:
                gradientDrawable.setOrientation(GradientDrawable.Orientation.RIGHT_LEFT);
                break;
            case B_T:
                gradientDrawable.setOrientation(GradientDrawable.Orientation.BOTTOM_TOP);
                break;
            case T_B:
                gradientDrawable.setOrientation(GradientDrawable.Orientation.TOP_BOTTOM);
                break;
            case BL_TR:
                gradientDrawable.setOrientation(GradientDrawable.Orientation.BL_TR);
                break;
            case BR_TL:
                gradientDrawable.setOrientation(GradientDrawable.Orientation.BR_TL);
                break;
            case TL_BR:
                gradientDrawable.setOrientation(GradientDrawable.Orientation.TL_BR);
                break;
            case TR_BL:
                gradientDrawable.setOrientation(GradientDrawable.Orientation.TR_BL);
                break;
        }
        this.setBackground(gradientDrawable);


    }

    public void setBkColor(int color) {
        this.bkColor = color;
        setBackground(null);
        invalidate();
    }


    public float getRound() {
        return round;
    }

    public void setRound(float round) {
        this.round = round;
        setBackground(null);
        invalidate();
    }

    public int getBkColor() {
        return bkColor;
    }

    public boolean isOpenGradient() {
        return isOpenGradient;
    }

    public void setOpenGradient(boolean openGradient) {
        isOpenGradient = openGradient;
        setBackground(null);
        invalidate();
    }

    public int getStartColor() {
        return startColor;
    }

    public void setStartColor(int startColor) {
        this.startColor = startColor;
        setBackground(null);
        invalidate();
    }

    public int getCenterColor() {
        return centerColor;
    }

    public void setCenterColor(int centerColor) {
        this.centerColor = centerColor;
        setBackground(null);
        invalidate();
    }

    public int getEndColor() {
        return endColor;
    }

    public void setEndColor(int endColor) {
        this.endColor = endColor;
        setBackground(null);
        invalidate();
    }

    public boolean isMut_grandiet() {
        return mut_grandiet;
    }

    public void setMut_grandiet(boolean mut_grandiet) {
        this.mut_grandiet = mut_grandiet;
        setBackground(null);
        invalidate();
    }

    public String getMut_grandiet_colors() {
        return mut_grandiet_colors;
    }

    public void setMut_grandiet_colors(String mut_grandiet_colors) {
        this.mut_grandiet_colors = mut_grandiet_colors;
        setBackground(null);
        invalidate();
    }

    public int getGrandientType() {
        return grandientType;
    }

    public void setGrandientType(int grandientType) {
        this.grandientType = grandientType;
        setBackground(null);
        invalidate();
    }

    public List<Integer> getColors() {
        return colors;
    }

    public void setColors(List<Integer> colors) {
        this.colors = colors;
        setBackground(null);
        invalidate();
    }

    public int getLinearGranditentDir() {
        return linearGranditentDir;
    }

    public void setLinearGranditentDir(int linearGranditentDir) {
        this.linearGranditentDir = linearGranditentDir;
        setBackground(null);
        invalidate();
    }

    public int getBorderColor() {
        return borderColor;
    }

    public void setBorderColor(int borderColor) {
        this.borderColor = borderColor;
        setBackground(null);
        invalidate();
    }

    public float getBorderWidth() {
        return borderWidth;
    }

    public void setBorderWidth(float borderWidth) {
        this.borderWidth = borderWidth;
        setBackground(null);
        invalidate();
    }
}
